<?php
// $Id: admin.php 382 2012-05-24 03:08:30Z tomqin@gmail.com $

class Action_Admin extends Action{
    public $user = null;

    public function __construct(){
        session_start();
    }

    //覆盖
    protected function init(){
        $this->initUser();
    }

    protected function initUser(){
        $this->user = User::getInstance();

        if(!$this->user->uid){
            $this->renderDialog('用户尚未登录，请重试！', '/admin/login');
        }else{
            if($this->user->status == USER_BUSINESS || $this->user->status == USER_ADMIN){
                ;//正常登陆用户
            }else{
                if($this->user->status == USER_DISABLED){
                    $message = sprintf('帐号`%s`被禁用！', $this->user->name);
                }elseif($this->user->status == USER_UNVERIFY){
                    $message = sprintf('帐号`%s`未验证！', $this->user->name);
                }elseif($this->user->status == USER_NORMAL){
                    $message = sprintf('帐号`%s`权限不足！', $this->user->name);
                }else{
                    $message = '未知错误！';
                }
                $message .= '<p>&nbsp;</p><p>&nbsp;</p><ul><li><a href="#">联系客服</a>，客服电话:'.App::getConfig('company.telphone').'</li><li><a href="/admin/login">重新登陆</a> / <a href="/">返回首页</a></li></ul>';
                $this->renderDialog($message, 'blank');
            }
        }
    }

    protected function render($tpl, $raw = false){
        //$isadmin = ($this->user->status == USER_ADMIN);
        View::setVar('theuser', $this->user);
        View::setVar('action',  $this->action);
        View::setVar('method',  $this->method);

        //$tpl = 'admin/'.($isadmin?'adm':'usr').'/'.$tpl;

        if($raw){
            View::addView($tpl);
        }else{
            View::setVar('isadmin', $this->user->status == USER_ADMIN);

            View::addView('admin/common_header');
            View::addView($tpl);
            View::addView('admin/common_footer');
        }
        View::display();
    }

    protected function renderDialog($message, $url_forward = 'goback', $dialog = '', $returnjs = ''){
        $this->setVar('message', $message);
        $this->setVar('url_forward', $url_forward);
        $this->setVar('dialog', $dialog);
        $this->setVar('returnjs', $returnjs);
        $this->render('admin/common_dialog', true);
        exit;
    }
}
